#include "serial_uart.h"

// 串口配置函数
int serial_init(const char *device) {
    int fd = open(device, O_RDWR | O_NOCTTY);
    if (fd < 0) {
        perror("open_port: Unable to open - ");
        return -1;
    }

    struct termios tty;
    memset(&tty, 0, sizeof tty);

    if (tcgetattr(fd, &tty) != 0) {
        perror("get_attr: tcgetattr failed");
        close(fd);
        return -1;
    }

    cfsetospeed(&tty, BAUDRATE);
    cfsetispeed(&tty, BAUDRATE);

    tty.c_cflag &= ~PARENB; // Clear parity bit
    tty.c_cflag &= ~CSTOPB; // Clear stop field
    tty.c_cflag &= ~CSIZE;  // Clear all bits that set the data size 
    tty.c_cflag |= CS8;     // 8 bits per byte
    tty.c_cflag &= ~CRTSCTS; // Disable RTS/CTS hardware flow control
    tty.c_cflag |= CREAD | CLOCAL; // Turn on READ & ignore control lines

    tty.c_lflag &= ~ICANON;
    tty.c_lflag &= ~ECHO; // Disable echo
    tty.c_lflag &= ~ECHOE; // Disable erasure
    tty.c_lflag &= ~ECHONL; // Disable new-line echo
    tty.c_lflag &= ~ISIG; // Disable interpretation of INTR, QUIT and SUSP

    tty.c_iflag &= ~(IXON | IXOFF | IXANY); // Turn off s/w flow ctrl
    tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL); // Disable any special handling of received bytes

    tty.c_oflag &= ~OPOST; // Prevent special interpretation of output bytes (e.g. newline chars)
    tty.c_oflag &= ~ONLCR; // Prevent conversion of newline to carriage return/line feed

    tty.c_cc[VTIME] = 10;    // Wait for up to 1s (10 deciseconds), returning as soon as any data is received.
    tty.c_cc[VMIN] = 0;

    if (tcsetattr(fd, TCSANOW, &tty) != 0) {
        perror("set_attr: tcsetattr failed");
        close(fd);
        return -1;
    }

    return fd;
}

// 串口写函数
int serial_write(int fd, char *buf, size_t count) {
    printf("fd=%d,buf=[%02x %02x %02x %02x %02x %02x %02x %02x]\n",fd,  buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]);
    return write(fd, buf, count);
}

// 串口读函数
int serial_read(int fd, char *buf, size_t count) {
    return read(fd, buf, count);
}

// 线程函数，用于接收指令并执行
void *thread_func(void *arg) {
    int fd = *(int *)arg;
    char buffer[256];
    while (1) {
        int n = serial_read(fd, buffer, sizeof(buffer) - 1);
        if (n > 0) {
            buffer[n] = '\0'; // 添加字符串结束符
            printf("Received: %s\n", buffer);
            // 这里可以添加解析和执行指令的代码
        }
    }
}

// int main(int argc, char*argv[]) {
//     int serial_fd = serial_init(DEVICE);
//     if (serial_fd < 0) {
//         return 1;
//     }

//     pthread_t thread_id;
//     if (pthread_create(&thread_id, NULL, thread_func, &serial_fd) != 0) {
//         perror("pthread_create failed");
//         close(serial_fd);
//         return 1;
//     }

//     char send_data[] = "Hello, World!";
//     while (1) {
//         serial_write(serial_fd, send_data, strlen(send_data));
//         sleep(1); // 每秒发送一次
//     }

//     pthread_join(thread_id, NULL);
//     close(serial_fd);
//     return 0;
// }